<div class="modal-header">
	<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
	<h4 class="modal-title"><font color="red">用户授权</font>&nbsp;</h4>
</div>
<div class="modal-body">
	<form class="form-horizontal" role="form" id="actForm">
	  <input name="userId" id="act-userId" type="hidden">
	<!-- Custom Tabs -->
		<div class="nav-tabs-custom">
			<ul class="nav nav-tabs">
				<li class="active"><a href="#tab_1" data-toggle="tab" onclick="getRoleListData();">拥有的角色</a></li>
				<li><a href="#tab_2" data-toggle="tab" onclick="getUserRightRole();">继承自角色的资源</a></li>
				<li><a href="#tab_3" data-toggle="tab" onclick="getActListData('0');">直接分配的操作权限</a></li>
				<li><a href="#tab_4" data-toggle="tab" onclick="getUserActsData();">全部操作权限</a></li>
				<li><a href="#tab_5" data-toggle="tab" onclick="getUserRightOrg();">数据权限-机构</a></li>
				<li><a href="#tab_6" data-toggle="tab" onclick="getUserRightDept();">数据权限-部门</a></li>
            </ul>           
	    <div class="tab-content">
			<div class="tab-pane active" id="tab_1">
				<table id="table1" class="table table-bordered table-hover" >
					<thead>
					<tr><th>角色名称</th>
						<th>角色标识</th><th>所属机构</th>
						<th>创建时间</th><th>操作</th>
					</tr>
					</thead>
					<tbody id="roleList"></tbody>
				</table>
			</div>
		<!-- /.tab-pane -->
			<div class="tab-pane" id="tab_2">
				<table id="table2" class="table table-bordered table-hover" >
					<thead>
					<tr><th>权限名称</th>
						<th>权限英文全称</th>
						<th>权限中文全称</th>
					</tr>
					</thead>
					<tbody id="actList"></tbody>
				</table>
			</div>
			<div class="tab-pane" id="tab_3">
				<table id="table3" class="table table-bordered table-hover" >
					<thead>
					<tr><th>权限名称</th>
						<th>权限英文全称</th>
						<th>操作</th>
					</tr>
					</thead>
					<tbody id="userActList"></tbody>
				</table>
			</div>
			<div class="tab-pane" id="tab_4">
				<table id="table4" class="table table-bordered table-hover" >
					<thead>
					<tr><th>权限名称</th>
						<th>权限英文全称</th>
						<th>权限中文全称</th>
					</tr>
					</thead>
					<tbody id="userActallList"></tbody>
				</table>
			</div>
			<!-- /.tab-pane -->
			<div class="tab-pane" id="tab_5">
				<table id="table5" class="table table-bordered table-hover" >
					<thead>
					<tr><th>机构名称</th>
						<th>机构代码</th>
						<th>机构描述</th>
						<th>操作</th>
					</tr>
					</thead>
					<tbody id="actOrgList"></tbody>
				</table>
			</div>
			<div class="tab-pane" id="tab_6">
				<table id="table6" class="table table-bordered table-hover" >
					<thead>
					<tr><th>部门名称</th>
						<th>部门代码</th>
						<th>所属机构标识</th>
						<th>操作</th>
					</tr>
					</thead>
					<tbody id="actDeptList"></tbody>
				</table>
			</div>
					              <!-- /.tab-pane -->
		</div>
					            <!-- /.tab-content -->
	</div>
					          <!-- nav-tabs-custom -->
	   <!-- div class="box-footer">
		  <button type="button" class="btn btn-primary pull-left" onclick="cannel('act_modal');">取消</button>
		  <button type="button" class="btn btn-primary pull-right" onclick="addAct('actForm');">提 交</button>
	   </div -->
    </form>
</div>
<script type="text/template" id="roleListData">
	  		<? for ( var i = 0; i < rows.length; i++) {?>
				<tr>
					<td style="display:none;"><?=rows[i].roleId?></td>
					<td><?=rows[i].roleEname?></td>
					<td><?=rows[i].roleCname?></td>
					<td><?=rows[i].roleRemark?></td>
					<td><?=rows[i].createTime?></td>
					<td><?if(rows[i].giveId){?>
                        <i class="fa fa-check fa-2x" ondblclick="giveRole(this,'<?=rows[i].roleEname?>','<?=rows[i].roleId?>');"></i>
                       <?}else{?>
                       <i class="fa fa-close fa-2x" ondblclick="giveRole(this,'<?=rows[i].roleEname?>','<?=rows[i].roleId?>');"></i>
                       <?}?></td>
				</tr>
     		<?}?>
	</script>
<script type="text/template" id="actListData">
	  		<? for ( var i = 0; i < rows.length; i++) {?>
				<tr>
					<td style="display:none;"><?=rows[i].actId?></td>
					<td><?=rows[i].actCname?></td>
					<td><?=rows[i].fullEname?></td>
					<td><?=rows[i].fullCname?></td>
				</tr>
     		<?}?>
	</script>
<script type="text/template" id="actOrgListData">
	  		<? for ( var i = 0; i < rows.length; i++) {?>
				<tr id="<?=rows[i].orgId?>" fatherid="<?=rows[i].fatherId?>">
					<td style="display:none;"><?=rows[i].orgId?></td>
                     <th style="display:none;"><?=rows[i].orgCname?></th>
					<th style="display:none;"><?=rows[i].treeLevel?></th>
					<th style="display:none;"><?=rows[i].fatherId?></th>
					<td><?if(rows[i].subCount>0){?>
							<i class="fa fa-folder fa-cyellow" onclick="getorgChildTableData('<?=rows[i].orgId?>','<?=rows[i].treeLevel?>',this);"></i>&nbsp;<?=rows[i].orgCname?>
						<?} else{?>		
							<i class="fa fa-file-text fa-cyellow" <?if(rows[i].fatherId!='0'){?>style="padding-left:10px;"<?}?> ></i>&nbsp;<?=rows[i].orgCname?>
						<?}?></td>
					<td><?=rows[i].orgEname?></td>
					<td><?=rows[i].orgRemark?></td>
                     <td><?if(rows[i].hasGive=="T"){?>
                        <i class="fa fa-check fa-2x" ondblclick="giveObjAct(this,'<?=rows[i].orgId?>','org');"></i>
                       <?}else{?>
                       <i class="fa fa-close fa-2x" ondblclick="giveObjAct(this,'<?=rows[i].orgId?>','org');"></i>
                       <?}?></td>
				</tr>
     		<?}?>
	</script>
	
<script type="text/template" id="actDeptListData">
	  		<? for ( var i = 0; i < rows.length; i++) {?>
				<tr>
					<td style="display:none;"><?=rows[i].deptId?></td>
					<td><?=rows[i].deptCname?></td>
					<td><?=rows[i].deptEname?></td>
					<td><?=rows[i].orgEname?></td>
                     <td><?if(rows[i].hasGive=="T"){?>
                        <i class="fa fa-check fa-2x" ondblclick="giveObjAct(this,'<?=rows[i].deptId?>','dept');"></i>
                       <?}else{?>
                       <i class="fa fa-close fa-2x" ondblclick="giveObjAct(this,'<?=rows[i].deptId?>','dept');"></i>
                       <?}?></td>
				</tr>
     		<?}?>
	</script>
	
	<script type="text/template" id="userActListData">
	  		<? for ( var i = 0; i < rows.length; i++) {?>
				<tr id="role_<?=rows[i].actId?>"  roleFatherId=<?=rows[i].fatherId?>>
					<th style="display:none;"><?=rows[i].actId?></th>
					<th style="display:none;"><?=rows[i].actCname?></th>
					<th style="display:none;"><?=rows[i].fatherId?></th>
					<td><?if(rows[i].subCount>0){?>
							<i class="fa fa-folder" onclick="getChildRoleActsData('<?=rows[i].actId?>','<?=rows[i].fullEname?>',this);">&nbsp;<?=rows[i].actCname?></i>
						<?} else{?>		
							<i class="fa fa-file"></i>&nbsp;<?=rows[i].actCname?>
						<?}?></td>
					<td><?=rows[i].fullEname?></td>
					<td><?if("T"==rows[i].hasGive){?>
                        <i class="fa fa-check fa-2x" ondblclick="giveActRole(this,'<?=rows[i].actId?>');"></i>
                       <?}else{?>
                       <i class="fa fa-close fa-2x" ondblclick="giveActRole(this,'<?=rows[i].actId?>');"></i>
                       <?}?></td>
				</tr>
     		<?}?>
	</script>
<script>
function giveRole(obj,roleEname,roleId){
   var flag = "add";
   if($(obj).hasClass('fa-check')){
	   flag = "del";
   }
   $.ajax({type: "post",
		url: ctx+"/res/uct/admin/roleToUser?flag="+flag+"&roleEname="+roleEname+"&roleId="+roleId+"&userId="+id,
		success: function(data, textStatus){
			if($(obj).hasClass('fa-check')){
				$(obj).attr("class","fa fa-close fa-2x");
			}else{
				$(obj).attr("class","fa fa-check fa-2x");
			}
		}
   });
}

function giveObjAct(obj,objId,giveType){
   var flag = "add";
   if($(obj).hasClass('fa-check')){
	   flag = "del";
   }
   $.ajax({type: "post",
		url: ctx+"/res/uct/admin/userDataGive?flag="+flag+"&giveType="+giveType+"&objId="+objId+"&userId="+id,
		success: function(data, textStatus){
			if($(obj).hasClass('fa-check')){
				$(obj).attr("class","fa fa-close fa-2x");
			}else{
				$(obj).attr("class","fa fa-check fa-2x");
			}
		}
   });
}

function getUserRightRole(){
    deleteAllRow('actList');
	$.ajax({ type:'post', url:ctx+'/res/uct/admin/userRightRoleData?userId='+id,
		   data:$('#actFrom').serialize(), dataType:'json',
		   success: function(myData){
			var data = baidu.template('actListData',eval(myData));  
			$("#actList").html(data);
		   }
	 });	
}
function getUserRightOrg(){
    deleteAllRow('actOrgList');
	$.ajax({ type:'post', url:ctx+'/res/uct/admin/userRightOrgData?userId='+id,
		   data:$('#actFrom').serialize(), dataType:'json',
		   success: function(myData){
		   //console.log(myData);
		   var myDatas = new Object();
		   myDatas.rows = [];
		   //20180806特殊处理，解决首次加载时，第一个是第二级显示问题
		   if(myData!=null&& myData.length>0){
		       for(var i=0;i<myData.length;i++){
					var fatherId = myData[i].fatherId;
					if(fatherId=="0"){
                        myDatas.rows[myDatas.rows.length]=myData[i];//首次只保留顶层机构
					}
			   }
		   }
		   //console.log(myDatas);
			var data = baidu.template('actOrgListData',eval(myDatas));
			$("#actOrgList").html(data);
		   }
	 });	
}
function getUserRightDept(){
    deleteAllRow('actDeptList');
	$.ajax({ type:'post', url:ctx+'/res/uct/admin/userRightDeptData?userId='+id,
		   data:$('#actFrom').serialize(), dataType:'json',
		   success: function(myData){
		    var myDatas = new Object();
		    myDatas.rows = myData;
			var data = baidu.template('actDeptListData',eval(myDatas));  
			$("#actDeptList").html(data);
		   }
	 });	
}

function getorgChildTableData(fid,treeLevel,obj){
   var url=ctx+'/res/uct/admin/userRightOrgData';
	$.ajax({
		   type:'post',url:url,
		   data:{'id':fid,'userId':id},
		   dataType:'json',
		   success: function(myData){
		   var myDatas = new Object();
		    myDatas.rows = myData;
		   console.log(myData);
			var data = baidu.template('actOrgListData',myDatas);  
			if($(obj).hasClass("fa-folder-open")){
				$(obj).attr("class","fa fa-folder  fa-cyellow");
				delorgChiled(fid);
			}else{
				$(obj).attr("class","fa fa-folder-open fa-cyellow");
				$("#"+fid).after(data);
				 $("[fatherid='"+fid+"']").each(function(i){
					var thisObj = $(this);
					var pd = "10px 20px 10px ";
					var num = pd+parseInt(treeLevel)*20+"px";
					$(thisObj).find("td").eq(0).css("padding",num);
				 });
			 }
		   }
		});	
  } 
  
  function delorgChiled(id){
	  $("[fatherid='"+id+"']").each(function(i){
			 var thisObj = $(this);
			 $(thisObj).remove();
			 var id = thisObj.attr("id");
			 delorgChiled(id);
		 });
  }
  
  
  function getActListData(fatherId){
	deleteAllRow('userActList');
	$("#act-userId").val(id);
	$.ajax({ type:'post', url:ctx+'/res/uct/admin/userRightAct?userId='+id+"&id="+fatherId,
		   data:$('#actFrom').serialize(), dataType:'json',
		   success: function(myData){
		    console.log(myData);
		    var myDatas = new Object();
		    myDatas.rows = myData;
			var data = baidu.template('userActListData',eval(myDatas));  
			$("#userActList").html(data);
		   }
	 });	
  } 
function giveActRole(obj,actId){
   var flag = "add";
   if($(obj).hasClass('fa-check')){
	   flag = "del";
   }
   $.ajax({type: "post",
		url: ctx+"/res/uct/admin/authorizeAct?actGiveType=user&flag="+flag+"&id="+actId+"&objId="+id,
		success: function(data, textStatus){
			if($(obj).hasClass('fa-check')){
				$(obj).attr("class","fa fa-close fa-2x");
			}else{
				$(obj).attr("class","fa fa-check fa-2x");
			}
		}
   });
}

function getChildRoleActsData(actId,treeLevel,obj){
var count =0;
var ss = treeLevel.split("/");
count = ss.length-1;
	$.ajax({
		   type:'post',
		   url:ctx+'/res/uct/admin/userRightAct',
		   data:{'id':actId,'userId':id},
		   dataType:'json',
		   success: function(myData){
		    var myDatas = new Object();
		    myDatas.rows = myData;
			var data = baidu.template('userActListData',myDatas); 
			if($(obj).hasClass("fa-folder-open")){
				$(obj).attr("class","fa fa-folder");
				delRoleActsChiled(actId);
			}else{
				$(obj).attr("class","fa fa-folder-open");
				$("#role_"+actId).after(data);
				 $("[roleFatherId='"+actId+"']").each(function(i){
					var thisObj = $(this);
					var pd = "10px 20px 10px ";
					var num = pd+(count*20)+"px";
					$(thisObj).find("td").eq(0).css("padding",num);
				 });
			 }
		   }
		});	
  } 
  function delRoleActsChiled(id){
	  $("[roleFatherId='"+id+"']").each(function(i){
			 var thisObj = $(this);
			 $(thisObj).remove();
			 var id = thisObj.attr("id");
			 delRoleActsChiled(id);
		 });
  }
  
  function getUserActsData(){
    deleteAllRow('userActallList');
	$.ajax({ type:'post', url:ctx+'/res/uct/admin/userRightAll?userId='+id,
		   data:$('#actFrom').serialize(), dataType:'json',
		   success: function(myData){
			var data = baidu.template('actListData',eval(myData));  
			$("#userActallList").html(data);
		   }
	 });	
}
</script>